package com.hl.sfzy.controller.content;


import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.content.FM;
import com.hl.sfzy.service.content.FMService;
import com.hl.sfzy.utils.FileUpUtils;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;


@RestController
@RequestMapping("fm")
public class FMController {
    @Autowired
    private FMService fmService;

    @Value("${fm.banner.img.dir.path}")
    private String bannerImgPath;


    @Value("${fm.audio.dir.path}")
    private String audioPath;

    @GetMapping("/fmlist")
    public PageInfo<FM> fmList(Integer pageNum, Integer pageSize) {
        System.out.println(pageNum);
        PageInfo<FM> allList = fmService.getAllList(pageNum, pageSize);
        return allList;
    }

    @GetMapping("/getFM/{id}")
    public R<FM> getFMById(@PathVariable("id") Long id) {
        FM fm = fmService.getFMById(id);
        return R.success(fm);
    }

    @PostMapping("/uploadimg")
    public R<String> uploadimg(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return R.failure("文件为空，请重新上传");
        }
        String fileName = FileUpUtils.fileUp(file, bannerImgPath);
        return R.success(bannerImgPath + fileName);
    }


    @PostMapping("/uploadaudio")
    public R<String> uploadaudio(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return R.failure("文件为空，请重新上传");
        }
        String fileName = FileUpUtils.fileUp(file, audioPath);
        return R.success(audioPath  + fileName);
    }


    @PostMapping("addFM")
    public R<String> addFm(@RequestBody FM fm) {
        fm.setFmStatus(1);
        fm.setCreateTime(LocalDateTime.now());
        int i = fmService.addFM(fm);
        if (i > 0) {
            return R.success();
        } else {
            return R.failure("保存失败");
        }
    }


    @GetMapping("/remove/{id}")
    public R<String> removeFm(@PathVariable("id") Long id) {
        int i = fmService.removeFM(id);
        if (i > 0) {
            return R.success();
        }
        return R.failure("删除失败");

    }

    @GetMapping("down")
    public void down(HttpServletResponse response,String file) throws IOException {
        // 原生servlet
        System.out.println(file);
        File file1 = new File(file);
        InputStream inputStream = new FileInputStream(file1);
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(inputStream, outputStream);
    }


    @GetMapping("downaudio")
    public void downaudio(HttpServletResponse response, String fileName) throws IOException {
        File file = new File(fileName);
        response.setContentType("audio/mpeg");
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
        response.setContentLength((int) file.length());
        try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream())) {
            byte[] buffer = new byte[8192];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
        }
    }


    @GetMapping("/search")
    public  PageInfo<FM> search(@RequestParam("q") String keyword, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime, Integer pageNum, Integer pageSize){
        System.out.println(keyword);

        PageInfo<FM> pageInfo = fmService.searchList(keyword, startTime, endTime, pageNum, pageSize);
        return  pageInfo;
    }


    @PutMapping("/updateFM")
    public R<String> updateFM(@RequestBody FM fm) {
        int i = fmService.updateFM(fm);
        if (i > 0) {
            return R.success("更新成功");
        }
        return R.failure("更新失败");
    }

}
